package com.yaojiaxun.logisticsPlatformSystem.pacong;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
/**
 * @Auther: yjx_j
 * @Date: 2019/1/13 12:20
 * @Description:
 */

public class TXT2DB
{
    private static Connection connection = null;

    public static void main(String[] args)
    {
        initDB();

        BufferedReader bufferedReader = null;
        try
        {
            bufferedReader = new BufferedReader(new FileReader(new File("d:\\CityInfo.txt")));
            String line = null;
            while ((line = bufferedReader.readLine()) != null)
            {
                inser2DB(getCityName(line), getCityLevel(line), getCityCode(line));
                System.out.println("处理中……");
            }
        } catch (FileNotFoundException e)
        {
            e.printStackTrace();
        } catch (IOException e)
        {
            e.printStackTrace();
        }
    }

    private static void initDB()
    {
        try
        {
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/LogisticsPlatform?characterEncoding=utf-8&useSSL=false", "root",
                    "123456");
        } catch (SQLException e)
        {
            e.printStackTrace();
        } catch (ClassNotFoundException e)
        {
            e.printStackTrace();
        }
    }

    private static String getCityName(String line)
    {
        return line.substring(0, line.indexOf("{"));
    }

    private static String getCityCode(String line)
    {
        return line.substring(line.indexOf("[") + 1, line.indexOf("]"));
    }

    private static int getCityLevel(String line)
    {
        return Integer.parseInt(line.substring(line.indexOf("{") + 1, line.indexOf("}")));
    }

    private static void inser2DB(String cityName, int cityLevel, String cityCode)
    {
        try
        {
            Statement createStatement = connection.createStatement();
            createStatement
                    .executeUpdate("insert into area(city_name_zh,city_level,city_code) values('"
                            + cityName + "'," + cityLevel + ",'" + cityCode + "')");
        } catch (SQLException e)
        {
            e.printStackTrace();
        }
    }
}
